home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Fish Fillets / script / gods / prog_ships.lua < prev   
Text File  |  2005-07-16  |  11KB  |  352 lines

  1.  
  2. local maxlodex=10
  3. local maxlodey=10
  4. local nlodi=7
  5. function getNShips()
  6.     return nlodi
  7. end
  8.  
  9. local nkostek = {4,4,4,6,6,5,7}
  10. local tvary = {
  11.               {{1,0},{0,1},{1,1},{2,1},{0,0},{0,0},{0,0}},
  12.               {{1,0},{0,1},{1,1},{2,1},{0,0},{0,0},{0,0}},
  13.               {{1,0},{0,1},{1,1},{2,1},{0,0},{0,0},{0,0}},
  14.               {{1,0},{0,1},{1,1},{2,1},{1,2},{3,1},{0,0}},
  15.               {{1,0},{0,1},{1,1},{2,1},{1,2},{3,1},{0,0}},
  16.               {{2,0},{0,1},{1,1},{2,1},{3,1},{0,0},{0,0}},
  17.               {{1,0},{3,0},{0,1},{1,1},{2,1},{3,1},{4,1}}}
  18.  
  19. -- -----------------------------------------------------------------
  20. local function umistilod(h, x, y, sm, cislo, zkouset)
  21.     local planek = room.planek
  22.     local px, py, pi, pj
  23.     local vysl = true
  24.  
  25.     for i = 1, nkostek[cislo] do
  26.         pi = tvary[cislo][i][1]
  27.         pj = tvary[cislo][i][2]
  28.         switch(sm){
  29.             [0] = function()
  30.                 px = x + pi
  31.                 py = y + pj
  32.             end,
  33.             [1] = function()
  34.                 px = x - pj
  35.                 py = y + pi
  36.             end,
  37.             [2] = function()
  38.                 px = x - pi
  39.                 py = y - pj
  40.             end,
  41.             [3] = function()
  42.                 px = x + pj
  43.                 py = y - pi
  44.             end,
  45.         }
  46.         if zkouset then
  47.             if px > 0 and px <= maxlodex and py > 0 and py <= maxlodey then
  48.                 vysl = vysl and planek[h][1][px][py] == 0 and (px == 1 or planek[h][1][px - 1][py] == 0) and (px == maxlodex or planek[h][1][px + 1][py] == 0) and (py == 1 or planek[h][1][px][py - 1] == 0) and (py == maxlodey or planek[h][1][px][py + 1] == 0)
  49.             else
  50.                 vysl = false
  51.             end
  52.         else
  53.             planek[h][1][px][py] = cislo
  54.         end
  55.     end
  56.  
  57.     return vysl
  58. end
  59.  
  60. -- -----------------------------------------------------------------
  61. function initLode()
  62.     room.planek = {}
  63.     local planek = room.planek
  64.     for player = 1, 2 do
  65.         planek[player] = {}
  66.         for hit = 1, 2 do
  67.             planek[player][hit] = {}
  68.             for x = 1, maxlodex do
  69.                 planek[player][hit][x] = {}
  70.                 for y = 1, maxlodey do
  71.                     planek[player][hit][x][y] = 0
  72.                 end
  73.             end
  74.         end
  75.     end
  76.  
  77.     local pompole = {}
  78.     for p = 1, maxlodex * maxlodey * 4 do
  79.         pompole[p] = {}
  80.     end
  81.     local pocet = 0
  82.     for h = 1, 2 do
  83.         repeat
  84.             for i = 1, maxlodex do
  85.                 for j = 1, maxlodey do
  86.                     planek[h][1][i][j] = 0
  87.                     planek[h][2][i][j] = 0
  88.                 end
  89.             end
  90.             local n = 8
  91.             repeat
  92.                 n = n - 1
  93.                 pocet = 0
  94.                 for i = 1, maxlodex do
  95.                     for j = 1, maxlodey do
  96.                         for sm = 0, 3 do
  97.                             if umistilod(h, i, j, sm, n, true) then
  98.                                 pocet = pocet + 1
  99.                                 pompole[pocet][1] = i
  100.                                 pompole[pocet][2] = j
  101.                                 pompole[pocet][3] = sm
  102.                             end
  103.                         end
  104.                     end
  105.                 end
  106.                 if pocet > 0 then
  107.                     local p = random(pocet) + 1
  108.                     umistilod(h, pompole[p][1], pompole[p][2], pompole[p][3], n, false)
  109.                 end
  110.             until pocet == 0 or n == 1
  111.         until pocet ~= 0
  112.     end
  113. end
  114.  
  115. -- -----------------------------------------------------------------
  116. local posltrefena = -1
  117. function getLastHit()
  118.     return posltrefena
  119. end
  120.  
  121. --[[
  122.  vysledek: 1 - voda, 2 - nemuze byt, 3 - zasah, 4 - potopena,
  123.             5 - podvod potopena, 6 - zasah podvod, 7 - potopena podvod
  124.             8 - to jsi uz rikal+voda, 9 - to uz jsi rikal+zasah
  125. ]]
  126. --[[
  127.   hrac1: je-li nacata: 70% zkusi pokracovat v nacate lodi, 10% zkusi neco jineho,
  128.                        20% uplne nahodne
  129.          neni-li nacata, 80% zkusi regulerne, 20% zkusi nahodne
  130.   hrac2: je-li nacata: 90% bude pokracovat, 10% zkusi jinam
  131.          pri pokracovani muze najit podvod
  132.          neni-li nacata, zkusi jinam
  133.   potopi-li hrac2 lod a je-li to posl. moznost, pak na 50% podvod potopena,
  134.   pokud je to jen prubezny zasah (ne prvni), pak 5% zasah podvod
  135. ]]
  136. function hrajlode(h)
  137.     local planek = room.planek
  138.     local pompole = {}
  139.     for p = 1, maxlodex * maxlodey do
  140.         pompole[p] = {}
  141.     end
  142.     local pocet
  143.     local divnych
  144.     local nacata, potopena
  145.     local cinnost
  146.     local vysl
  147.     local lod
  148.     local sx, sy
  149.  
  150.     nacata = false
  151.     for i = 1, maxlodex do
  152.         for j = 1, maxlodey do
  153.             if planek[h][2][i][j] == 3 then
  154.                 nacata = true
  155.             end
  156.         end
  157.     end
  158.     if h == 1 then
  159.         if nacata then
  160.             local rand100 = random(100)
  161.             if isRange(rand100, 0, 49) then
  162.                 cinnost = 1
  163.             elseif isRange(rand100, 50, 69) then
  164.                 cinnost = 2
  165.             else
  166.                 cinnost = 3
  167.             end
  168.         else
  169.             local rand100 = random(100)
  170.             if isRange(rand100, 0, 69) then
  171.                 cinnost = 2
  172.             else
  173.                 cinnost = 3
  174.             end
  175.         end
  176.     elseif nacata then
  177.         local rand100 = random(100)
  178.         if isRange(rand100, 0, 89) then
  179.             cinnost = 1
  180.         else
  181.             cinnost = 2
  182.         end
  183.     else
  184.         cinnost = 2
  185.     end
  186.     pocet = 0
  187.     sx = -1
  188.     switch(cinnost){
  189.         [1] = function()
  190.             for i = 1, maxlodex do
  191.                 for j = 1, maxlodey do
  192.                     if planek[h][2][i][j] == 0 then
  193.                         if i > 1 and planek[h][2][i - 1][j] == 3 or i < maxlodex and planek[h][2][i + 1][j] == 3 or j > 1 and planek[h][2][i][j - 1] == 3 or j < maxlodey and planek[h][2][i][j + 1] == 3 then
  194.                             pocet = pocet + 1
  195.                             pompole[pocet][1] = i
  196.                             pompole[pocet][2] = j
  197.                         end
  198.                     end
  199.                 end
  200.             end
  201.             if pocet == 0 then
  202.                 for i = 1, maxlodex do
  203.                     for j = 1, maxlodey do
  204.                         if planek[h][2][i][j] == 6 then
  205.                             pocet = pocet + 1
  206.                             pompole[pocet][1] = i
  207.                             pompole[pocet][2] = j
  208.                         end
  209.                     end
  210.                 end
  211.             end
  212.             if pocet > 0 then
  213.                 pocet = random(pocet) + 1
  214.                 sx = pompole[pocet][1]
  215.                 sy = pompole[pocet][2]
  216.             end
  217.         end,
  218.         [2] = function()
  219.             if h == 2 and random(100) < 75 then
  220.                 for i = 2, maxlodex - 1 do
  221.                     for j = 2, maxlodey - 1 do
  222.                         if planek[h][2][i][j] == 0 then
  223.                             pocet = pocet + 1
  224.                             pompole[pocet][1] = i
  225.                             pompole[pocet][2] = j
  226.                         end
  227.                     end
  228.                 end
  229.             end
  230.             if pocet == 0 then
  231.                 for i = 1, maxlodex do
  232.                     for j = 1, maxlodey do
  233.                         if planek[h][2][i][j] == 0 then
  234.                             pocet = pocet + 1
  235.                             pompole[pocet][1] = i
  236.                             pompole[pocet][2] = j
  237.                         end
  238.                     end
  239.                 end
  240.             end
  241.             if pocet > 0 then
  242.                 pocet = random(pocet) + 1
  243.                 sx = pompole[pocet][1]
  244.                 sy = pompole[pocet][2]
  245.             end
  246.         end,
  247.     }
  248.     if sx == -1 then
  249.         sx = random(maxlodex) + 1
  250.         sy = random(maxlodey) + 1
  251.     end
  252.     lod = planek[3 - h][1][sx][sy]
  253.     if isRange(lod, 1, 3) then
  254.         posltrefena = 3 + random(2)
  255.     elseif isRange(lod, 4, 5) then
  256.         posltrefena = 0
  257.     elseif lod == 6 then
  258.         posltrefena = 1
  259.     elseif lod == 7 then
  260.         posltrefena = 2
  261.     else
  262.         posltrefena = -1
  263.     end
  264.     if planek[h][2][sx][sy] ~= 0 and planek[h][2][sx][sy] ~= 2 and h == 1 then
  265.         if lod == 0 then
  266.             vysl = 8
  267.         else
  268.             vysl = 9
  269.         end
  270.     elseif lod == 0 then
  271.         vysl = 1
  272.         planek[h][2][sx][sy] = 1
  273.     else
  274.         potopena = true
  275.         for i = 1, maxlodex do
  276.             for j = 1, maxlodey do
  277.                 if i ~= sx or j ~= sy then
  278.                     if planek[3 - h][1][i][j] == lod and planek[h][2][i][j] ~= 3 then
  279.                         potopena = false
  280.                     end
  281.                 end
  282.             end
  283.         end
  284.         if potopena then
  285.             vysl = 4
  286.         else
  287.             vysl = 3
  288.         end
  289.         if planek[h][2][sx][sy] == 6 then
  290.             planek[h][2][sx][sy] = vysl
  291.             vysl = vysl + 3
  292.         else
  293.             planek[h][2][sx][sy] = vysl
  294.         end
  295.         if vysl == 4 and h == 2 then
  296.             pocet = 0
  297.             for i = 1, maxlodex do
  298.                 for j = 1, maxlodey do
  299.                     if i ~= sx and j ~= sy and planek[3 - h][1][i][j] == lod then
  300.                         if i > 1 and (planek[h][2][i - 1][j] == 0 or planek[h][2][i - 1][j] == 6) or i < maxlodex and (planek[h][2][i + 1][j] == 0 or planek[h][2][i + 1][j] == 6) or j > 1 and (planek[h][2][i][j - 1] == 0 or planek[h][2][i][j - 1] == 6) or j < maxlodey and (planek[h][2][i][j + 1] == 0 or planek[h][2][i][j + 1] == 6) then
  301.                             pocet = pocet + 1
  302.                         end
  303.                     end
  304.                 end
  305.             end
  306.             if pocet == 0 then
  307.                 vysl = 5
  308.             end
  309.         end
  310.         if vysl == 3 and h == 2 then
  311.             pocet = 0
  312.             for i = 1, maxlodex do
  313.                 for j = 1, maxlodey do
  314.                     if i ~= sx and j ~= sy and planek[3 - h][1][i][j] == lod then
  315.                         if planek[h][2][i][j] == 3 then
  316.                             pocet = pocet + 1
  317.                         end
  318.                     end
  319.                 end
  320.             end
  321.             if pocet > 0 and random(100) < 10 then
  322.                 vysl = 1
  323.                 planek[h][2][sx][sy] = 6
  324.             end
  325.         end
  326.         if planek[h][2][sx][sy] == 4 then
  327.             for i = 1, maxlodex do
  328.                 for j = 1, maxlodey do
  329.                     if planek[3 - h][1][i][j] == lod then
  330.                         planek[h][2][i][j] = 4
  331.                         if i > 1 and planek[h][2][i - 1][j] == 0 then
  332.                             planek[h][2][i - 1][j] = 2
  333.                         end
  334.                         if i < maxlodex and planek[h][2][i + 1][j] == 0 then
  335.                             planek[h][2][i + 1][j] = 2
  336.                         end
  337.                         if j > 1 and planek[h][2][i][j - 1] == 0 then
  338.                             planek[h][2][i][j - 1] = 2
  339.                         end
  340.                         if j < maxlodey and planek[h][2][i][j - 1] == 0 then
  341.                             planek[h][2][i][j + 1] = 2
  342.                         end
  343.                     end
  344.                 end
  345.             end
  346.         end
  347.     end
  348.     return vysl, sx, sy
  349. end
  350.  
  351.  
  352.